﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuestionSetLibrary;

namespace FormsControlLibraryForWindowsAndWeb
{
    public partial class UserControlForAdmin : UserControl
    {
        static int numberOfChoice = 2;
        public UserControlForAdmin()
        {
            InitializeComponent();
        }

        private void comboBox_Question_Type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxQuestionType.Text == "Text question")
            {
                panel2.Visible = false;
            }
            if (comboBoxQuestionType.Text == "Multiple choice question")
            {
                panel2.Visible = true;
            }
        }

        private void button_Add_Choice_Click(object sender, EventArgs e)
        { 
            if (numberOfChoice == 4)
            {
                MakeControlVisible(radioButtonChoice5, textBoxChoice5,true);
            }
            if (numberOfChoice == 3)
            {
                MakeControlVisible(radioButtonChoice4, textBoxChoice4, true);
            }
            if (numberOfChoice == 2) {
                MakeControlVisible(radioButtonChoice3, textBoxChoice3, true); 
            }
            

        }

        private void MakeControlVisible(RadioButton rb,TextBox tb,bool b)
        {
            rb.Visible = b;
            tb.Visible = b;
            if (b)
            {
                numberOfChoice++;
            }
            else {
                numberOfChoice--;
            }
        }

        private void button_Remove_Choice_Click(object sender, EventArgs e)
        {
            removeChoice();
        }

        private void removeChoice()
        {
            if (numberOfChoice == 3)
            {
                MakeControlVisible(radioButtonChoice3, textBoxChoice3, false);
            }
            if (numberOfChoice == 4)
            {
                MakeControlVisible(radioButtonChoice4, textBoxChoice4, false);
            }
            if (numberOfChoice == 5)
            {
                MakeControlVisible(radioButtonChoice5, textBoxChoice5, false);
            }
            
        }

        private void textBox_Choice_3_VisibleChanged(object sender, EventArgs e)
        {
            textBoxChoice3.Text = "";
        }

        private void textBox_Choice_4_TextChanged(object sender, EventArgs e)
        {
            textBoxChoice4.Text = "";
        }

        private void textBox_Choice_5_TextChanged(object sender, EventArgs e)
        {
            textBoxChoice4.Text = "";
        }

        private void buttonSignOut_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void UserControlForAdmin_Load(object sender, EventArgs e)
        {
            string json = GetHTTP.GetWebContent("http://localhost:50037/GetQuestionSet/adminId=1/all");
            try
            {
                QuestionSet[] setList = JsonCovertor.GetQuestionSetFormJSON(json);
                List<string> questionSetNameList = new List<string>();
                for (int i = 0; i < setList.Length; i++)
                {
                    questionSetNameList.Add(setList[i].setName);
                }
            }
            catch (Exception)
            {
                
                MessageBox.Show("Error, continue without getting JSON.");
            }
        }


    }
}
